package com.mo.lib.utils

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.view.View
import androidx.fragment.app.Fragment

/**
 * @ author：mo
 * @ data：2022/4/18:14:04
 * @ 功能：
 */


/**获取Context对象 */
fun Any.getContext(): Context? {
    return when (this) {
        is Activity       -> return this
        is Fragment       -> return this.activity
        is View           -> return this.context
        is ContextWrapper -> (this as ContextWrapper).baseContext
        else              -> null
    }
}
fun Context.getActivity(): Activity? {
    var context = this
    do {
        context = if (context is Activity) {
            return context
        } else if (context is ContextWrapper) {
            context.baseContext
        } else {
            return null
        }
    } while (context != null)
    return null
}

fun Context.finishActivity() {
    getActivity()?.let {
        it.finish()
    }
}

